package com.gerfmarquez.nymph.ui.threads;

import java.util.ArrayList;
import java.util.concurrent.ConcurrentLinkedQueue;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Handler;

import com.gerfmarquez.nymph.R;
import com.gerfmarquez.nymph.ui.seekbar.ActiveSeekBarMusicNote;
import com.gerfmarquez.nymph.ui.seekbar.VerticalSeekBar;

public class UpdatePlayStateNotesThread extends Thread {
	Context context;
	ArrayList<VerticalSeekBar> verticalSeekBars;
	Resources r;
	private static String TAG = "UpdatePlayStateNotesThread"; 
	Handler uiHandler;
	Handler activeNoteChanger;
	
	public Handler getActiveNoteChanger() {
		return activeNoteChanger;
	}
	Drawable prDrInactive;
	Drawable prDrActive;
	
	boolean allViewsProcessed;
	
	public void setAllViewsProcessed(boolean allViewsProcessed) {
		this.allViewsProcessed = allViewsProcessed;
	}
	public UpdatePlayStateNotesThread(Context context,ArrayList<VerticalSeekBar> verticalSeekBars, Handler handler) {
		this.uiHandler = handler;
		this.context = context;
		this.verticalSeekBars = verticalSeekBars;
		r = context.getResources();
		activeNoteChanger = new Handler();
	}
	@Override
	public void run() {
		while(!allViewsProcessed) {
			try {
				Thread.sleep(50);
			} catch(InterruptedException ie) {
				
			}
		}
		

	}
	public void changeToPlayState(ConcurrentLinkedQueue<ActiveSeekBarMusicNote> activeMusicNoteQueue, final int lastNote) {
		while(!activeMusicNoteQueue.isEmpty()) {
//			Log.d(TAG,"New queue");
			final ActiveSeekBarMusicNote active = activeMusicNoteQueue.poll();
			uiHandler.post(new Runnable() {
				
				public void run() {
						if(lastNote != active.getIndex()  && (lastNote > active.getIndex())){
							VerticalSeekBar vs = verticalSeekBars.get(active.getIndex());
//							Log.e(TAG,"Last note: "+lastNote+" index: "+active.getIndex());
							Rect bounds = vs.getProgressDrawable().copyBounds();
							int progr = vs.getProgress();
							int max = vs.getMax();
							int width = vs.getWidth();
							int height = vs.getHeight();
							prDrActive = r.getDrawable(R.drawable.seekbar_active_progress);
							prDrActive = r.getDrawable(R.drawable.seekbar_active_progress);
							vs.setProgressDrawable(prDrActive);
							vs.getProgressDrawable().setBounds(bounds);
							vs.setMax(max);
							vs.setProgress(1);
							vs.setProgress(progr);
							vs.onSizeChanged(width, height);
							vs.refreshDrawableState();
						}
						if(active.getIndex() != 0 && (lastNote > active.getIndex())) {
							VerticalSeekBar vsBack = verticalSeekBars.get(active.getIndex()-1);
							prDrInactive = r.getDrawable(R.drawable.seekbar_inactive_progress);
							prDrInactive = r.getDrawable(R.drawable.seekbar_inactive_progress);
							vsBack.setProgressDrawable(prDrInactive);
							Rect vsBackbounds = vsBack.getProgressDrawable().copyBounds();
							int vsBackvsBackprogr = vsBack.getProgress();
							int max = vsBack.getMax();
							int vsBackwidth = vsBack.getWidth() - (vsBack.getPaddingLeft()+ vsBack.getPaddingRight());
							int vsBackheight = vsBack.getHeight();
							vsBack.getProgressDrawable().setBounds(vsBackbounds);
							vsBack.setMax(max);
							vsBack.setProgress(1);
							vsBack.setProgress(vsBackvsBackprogr);
							vsBack.onSizeChanged(vsBackwidth, vsBackheight);
							vsBack.refreshDrawableState();
//							Log.e(TAG, "Disabling last seekbar::::::::::::::"+(active.getIndex()-1));
						}
					
				}
			});
			
		}
	}
	
	public void updateLast() {
		uiHandler.post(new Runnable() {
			
			@Override
			public void run() {
				VerticalSeekBar vs = verticalSeekBars.get(verticalSeekBars.size()-1);
				Rect bounds = vs.getProgressDrawable().copyBounds();
				int progr = vs.getProgress();
				int max = vs.getMax();
				int width = vs.getWidth() - (vs.getPaddingLeft()+ vs.getPaddingRight());
				int height = vs.getHeight();
				prDrInactive = r.getDrawable(R.drawable.seekbar_inactive_progress);
				prDrInactive = r.getDrawable(R.drawable.seekbar_inactive_progress);
				vs.setProgressDrawable(prDrInactive);
				vs.getProgressDrawable().setBounds(bounds);
				vs.setMax(max);
				vs.setProgress(1);
				vs.setProgress(progr);
				vs.onSizeChanged(width, height);
				vs.refreshDrawableState();
			}
		});
	}
		
}
	
